import { Injectable } from '@angular/core';
import { WindowConfigDto, WindowDto } from '../dto/windowDto';
import { DomSanitizer } from '@angular/platform-browser';
import { W } from '@angular/cdk/keycodes';
import { BehaviorSubject } from 'rxjs';
import { OpenApiService } from './open-api.service';

@Injectable({
  providedIn: 'root'
})
export class WindowsManagerService {

  constructor() { }

  // 激活状态
  activateStatus = true;

  // 顶层窗口
  topWindow?:WindowDto; 

  showOrHide(id: number) {
    // step 1: 找到对应的windowDto
    let windowDto = this.windowDtos.value.find(s => s.id == id);
    if (!windowDto) { return; }
    // step 2: 设置显示
    windowDto!.isHide = !windowDto!.isHide;
    if(windowDto.isHide)
      {
          this.cancelTop(id);
      }
      else{
          this.setTop(windowDto.id);
      }
  }

  closeAll() {
    this.windowDtos.next([]);
  }

  hide(id: number) {
    // step 1: 找到对应的windowDto
    let windowDto = this.windowDtos.value.find(s => s.id == id);
    if (!windowDto) { return; }
    // step 2: 设置隐藏
    windowDto!.isHide = true; 
    this.cancelTop(id);
  }

  show(id: number) {
    // step 1: 找到对应的windowDto
    let windowDto = this.windowDtos.value.find(s => s.id == id);
    if (!windowDto) { return; }
    // step 2: 设置显示
    windowDto!.isHide = false;
    this.setTop(windowDto.id); 
  }

  sizeCommon(id: number) {
    // step 1: 找到对应的windowDto
    let windowDto = this.windowDtos.value.find(s => s.id == id);
    if (!windowDto) { return; }
    // step 2: 设置最大化
    windowDto!.isSizeMax = false;
  }

  sizeMax(id: number) {
    // step 1: 找到对应的windowDto
    let windowDto = this.windowDtos.value.find(s => s.id == id);
    if (!windowDto) { return; }
    // step 2: 设置最大化
    windowDto!.isSizeMax = true;

  }

  windowDtos = new BehaviorSubject<WindowDto[]>([]);

  maxId = 0;

  open(title: string, url: string,cfg?: WindowConfigDto) {

    // step 1: 构建windowDto 
    let windowDto = new WindowDto();
    windowDto.id = this.generateId();
    windowDto.title = title; 
    windowDto.width = "900px";
    windowDto.height = "600px";
    windowDto.left = 0;
    windowDto.top = 0;
    windowDto.zIndex = (this.topWindow?.zIndex??0) + 1;
    windowDto.url = url;
    windowDto.taskbarSort = windowDto.id;

    
    if(cfg?.width) { windowDto.width = cfg.width; }
    if(cfg?.height) { windowDto.height = cfg.height; }
    if(cfg?.left) { windowDto.left = cfg.left; }
    if(cfg?.top) { windowDto.top = cfg.top; }
    if(cfg?.title) { windowDto.title = cfg.title; }
    if(cfg?.iconUrl) { windowDto.iconUrl = cfg.iconUrl; }

    let x = (window.innerWidth - parseInt(windowDto.width, 10)) / 2;
    let y = (window.innerHeight - parseInt(windowDto.height, 10)) / 2;
    windowDto.position = { x, y };

    // step 2: 添加到windowDtos
    this.windowDtos.next([...this.windowDtos.value, windowDto]);
    this.topWindow = windowDto; 
  }

  setTop(id: number){
    // step 1: check
    if(this.topWindow!.id == id){ return; }
    // step 2: 设置为最大
    const windowDto = this.windowDtos.value.find(s=>s.id == id);
    if(windowDto){  
      windowDto!.zIndex = this.topWindow!.zIndex + 1;  
      this.topWindow = windowDto;
    } 
  }

  cancelTop(id: number){

    // step 1: check
    if((this.topWindow?.id) !== id){ return;}

    // step 2: 设置当前xi为0
    let indexW = this.windowDtos.value.find(s=>s.id == id);
    if(!!window) { return;}
    indexW!.zIndex = 0

    // step 2: 获取新顶层
    let ws = this.windowDtos.value.sort((a,b)=> b.zIndex - a.zIndex);
    this.topWindow = ws[0];
  }

  close(id: number) {
    // step 1: 从windowDtos中移除
    let currentWindowDtos = this.windowDtos.value.filter((s) => {
      return s.id != id
    })
    this.windowDtos.next(currentWindowDtos);
  }

  generateId(): any {
    return ++this.maxId
  }


}
